package cn.kgc.service;

import cn.kgc.dao.*;
import cn.kgc.pojo.Category;
import cn.kgc.pojo.NewsDetail;
import cn.kgc.util.DataBaseUtil_jndi;
import cn.kgc.util.DataBaseUtil_single;
import cn.kgc.util.DatabaseUtil;
import cn.kgc.util.PageSupport;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDetailServiceImpl implements NewsDetailService {

    NewsDetailDao newsDetailDao =new NewsDetailDaoImpl();


    @Override
    public List<NewsDetail> getAllNewsDetail() {
        Connection connection=DataBaseUtil_single.getInstance().getConnect();
        List<NewsDetail> newsDetailList=  newsDetailDao.getNewsDestailList(connection);
        DatabaseUtil.closeResult(connection,null,null);
        return  newsDetailList;
    }


    @Override
    public int addNews(NewsDetail newsDetail) {
        Connection connection=DataBaseUtil_single.getInstance().getConnect();
        int result=0;
        try {
            connection.setAutoCommit(false);
            result =newsDetailDao.insertNewsdetail(connection,newsDetail);
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            DatabaseUtil.closeResult(connection,null,null);
        }
            return result;
    }

    @Override
    public PageSupport<NewsDetail> getNewsDetailListByPage(String title, Integer categoryId, PageSupport page) {
         Connection connection=DataBaseUtil_single.getInstance().getConnect();


            List<NewsDetail> newsDetailList=new ArrayList<>();
            int count=0;
            try {
                connection.setAutoCommit(true);
                newsDetailList =newsDetailDao.getNewsDetailList(connection,title,categoryId,(page.getPageNo()-1)*page.getPageSize(),page.getPageSize());
                count=  newsDetailDao.getNewsListCount(connection,title,categoryId);
                page.setData(newsDetailList);
                page.setTotalcount(count);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                DatabaseUtil.closeResult(connection,null,null);
        }
        return page;
    }

    @Override
    public NewsDetail getNewsDetailById(Integer id) {

        Connection connection=DataBaseUtil_single.getInstance().getConnect();
        NewsDetail newsDetail=  newsDetailDao.getNewsDetailById(connection,id);
        DatabaseUtil.closeResult(connection,null,null);
        return  newsDetail;

    }

    @Override
    public boolean updateNews(NewsDetail newsDetail) {
        Connection connection=DataBaseUtil_single.getInstance().getConnect();
        try {
            connection.setAutoCommit(false);
            int rs=  newsDetailDao.updateNewsDetail(connection,newsDetail);
            if(rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DatabaseUtil.closeResult(connection,null,null);
        }
        return false;
    }

    @Override
    public boolean deleteNews(Integer id) {
        Connection connection=DataBaseUtil_single.getInstance().getConnect();
        try {
            connection.setAutoCommit(false);
            int rs=  newsDetailDao.deleteNewsDetail(connection,id);
            if(rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DatabaseUtil.closeResult(connection,null,null);
        }
        return false;
    }


}
